package com.faxsun.controller.uc;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.broadleafcommerce.common.config.RuntimeEnvironmentPropertiesManager;
import org.broadleafcommerce.common.web.controller.BroadleafAbstractController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faxsun.captcha.service.FSCaptchaService;
import com.faxsun.captcha.service.FSDigitalCaptchaServiceImpl;

@Controller
@RequestMapping("/captcha")
public class CaptchaController extends BroadleafAbstractController {

	@Resource(name="blConfigurationManager")
	protected RuntimeEnvironmentPropertiesManager runtimeEnvironment;
	
	protected FSCaptchaService captchaService=FSDigitalCaptchaServiceImpl.getSingleInstance();
	
	/**
	 * 发送验证码到手机，并将手机号存储在当前session中
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sendCaptcha")
	@ResponseBody
	public boolean sendCaptchaToMobile(HttpServletRequest request,
			Model model){
		boolean isSendSucc=false;
		String mobileCaptchaEnabled=runtimeEnvironment.getProperty("captcha.mobile", "enable");
		boolean captchaEnable=false;
		
		try{
			captchaEnable=Boolean.parseBoolean(mobileCaptchaEnabled);
		}
		catch(Exception e){
			
		}
		
		if(!captchaEnable){
			return isSendSucc;
		}
		
		String mobile=request.getParameter("mobile");
		if(mobile!=null && !mobile.isEmpty()){

			request.getSession().setAttribute("captchaMobile", mobile);
			String ret=captchaService.getNumberCodeForID(request.getSession().getId(), mobile);
			if(ret!=null && !ret.isEmpty()){
				isSendSucc= true;
			}
			else{
				isSendSucc= false;
			}
		}
		return isSendSucc;
	}
}
